package com.example.guiguihomemedical.service.impl;

import com.example.guiguihomemedical.entity.PersonalInfo;
import com.example.guiguihomemedical.entity.Record;
import com.example.guiguihomemedical.entity.Signing;
import com.example.guiguihomemedical.entity.dto.HealthRecordTotal;
import com.example.guiguihomemedical.mapper.RecordListMapper;
import com.example.guiguihomemedical.service.DoctorService;
import com.example.guiguihomemedical.service.PersonalInfoService;
import com.example.guiguihomemedical.service.RecordService;
import com.example.guiguihomemedical.service.HealthRecordTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class HealthRecordTotalServiceImpl implements HealthRecordTotalService {
    @Autowired
    private RecordService recordService;
    @Autowired
    private RecordListMapper recordListMapper;
    @Autowired
    private PersonalInfoService personalInfoService;
    @Autowired
    private DoctorService doctorService;


    @Override
    public HealthRecordTotal gethealthRecordTotal(String idcard){

        Record record = recordService.findByIdcard(idcard);
        //人群分类
        List<String> listenerNames = recordListMapper.getListenerNamesById(idcard);
        //    获取签约状态
        Signing signing = recordListMapper.getEndTimeById(idcard);
        Integer status = 0;
        if (signing != null) {
            LocalDateTime now = LocalDateTime.now();
            Instant instant = signing.getEndtime().toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime endTime = instant.atZone(zoneId).toLocalDateTime();
            if (now.isAfter(endTime)) {
                status = 2;
            } else {
                status = 1;
            }
        }
        //    计算年龄
        String birthdayString=idcard.substring(6,14);
        //LocalDate birthday=LocalDate.parse(birthdayString, DateTimeFormatter.ofPattern("yyyyMMdd"));

        PersonalInfo personalInfo = new PersonalInfo();
        personalInfo = personalInfoService.getPersonalInfo(idcard);
        if(personalInfo==null){
            personalInfo = new PersonalInfo(idcard,1,null,null,null,null,0,0,0,null);
        }
        //医生名字
        String doctorname = doctorService.findByDoctorId(record.getDoctorid()).getName();
        HealthRecordTotal healthRecordTotal = new HealthRecordTotal(
                listenerNames,
                doctorname,
                status,
                record.getTime(),
                idcard,
                record.getName(),
                record.getNation(),
                record.getDetailedresidence(),
                record.getPhone(),
                record.getSex(),
                personalInfo.getRegisteredType(),
                personalInfo.getBloodType(),
                personalInfo.getOccupation(),
                personalInfo.getMaritalStatus(),
                personalInfo.getMedicalPaymentMethod(),
                personalInfo.getDrugAllergyHistory(),
                personalInfo.getExposureHistory(),
                personalInfo.getSurgery(),
                personalInfo.getReason()
        );

        return healthRecordTotal ;
    }
    @Override
    public void addPersonalInfo(HealthRecordTotal healthRecordTotal){
        PersonalInfo personalInfo = new PersonalInfo(
                healthRecordTotal.getIdcard(),
                healthRecordTotal.getRegisteredType(),
                healthRecordTotal.getBloodType(),
                healthRecordTotal.getOccupation(),
                healthRecordTotal.getMaritalStatus(),
                healthRecordTotal.getMedicalPaymentMethod(),
                healthRecordTotal.getDrugAllergyHistory(),
                healthRecordTotal.getExposureHistory(),
                healthRecordTotal.getSurgery(),
                healthRecordTotal.getReason()
        );

        Record record = new Record(
                healthRecordTotal.getIdcard(),
                healthRecordTotal.getName(),
                healthRecordTotal.getNation(),
                null,
                healthRecordTotal.getDetailedresidence(),
                null,
                null,
                healthRecordTotal.getPhone(),
                healthRecordTotal.getSex(),
                null
        );

        if(personalInfoService.getPersonalInfo(healthRecordTotal.getIdcard())==null){
            personalInfoService.instrtPersonalInfo(personalInfo);
        }else{
            personalInfoService.updataPersonalInfo(personalInfo);
        }
        recordService.updateRecord_h(record);

    }
}
